package com.example.a33mvvm.live;

import androidx.lifecycle.MutableLiveData;

import com.example.base_mvvm.base.BaseRepo;
import com.example.base_mvvm.base.view.GiftEntity;
import com.example.base_mvvm.entity.FollowEntity;
import com.example.base_mvvm.money.Money;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.RequestBody;

public class LiveRepo extends BaseRepo {
    public void follow(RequestBody body, MutableLiveData<FollowEntity> success,MutableLiveData<String> failure){
        apiService.follow(body)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<FollowEntity>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(FollowEntity followEntity) {
                        success.setValue(followEntity);
                    }

                    @Override
                    public void onError(Throwable e) {
                        failure.setValue(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
    public void noFollow(RequestBody body, MutableLiveData<FollowEntity> success,MutableLiveData<String> failure){
        apiService.noFollow(body)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<FollowEntity>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(FollowEntity followEntity) {
                        success.setValue(followEntity);
                    }

                    @Override
                    public void onError(Throwable e) {
                        failure.setValue(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
    public void getGift(MutableLiveData<GiftEntity> success,MutableLiveData<String> failure){
        apiService.getGift()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<GiftEntity>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(GiftEntity giftEntity) {
                        success.setValue(giftEntity);
                    }

                    @Override
                    public void onError(Throwable e) {
                        failure.setValue(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }


    public void getMoney(String username, MutableLiveData<Money> success, MutableLiveData<String> failure){
                if( moneyDao.query(username)!=null){
                    success.setValue(moneyDao.query(username));
                }else {
                    failure.setValue("没钱石佳晨");
                }
    }

    public void updateMoney(Money money){
        moneyDao.update(money);
    }


}
